package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private DtsCategoryMapper categoryMapper;

    @Override
    public IPage<DtsCategory> queryCategoryPage(Integer id, String name, Integer page, Integer limit, String sort, String order) {

        IPage<DtsCategory> ipage = new Page<>(page, limit);

        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);

        if (!StringUtils.isEmpty(id)){
            queryWrapper.eq("id",id);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.eq("name",name);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(sort);
            } else {
                queryWrapper.orderByDesc(sort);
            }
        }

        IPage<DtsCategory> resultPage = categoryMapper.selectPage(ipage, queryWrapper);

        return resultPage;
    }

    @Override
    public IPage<DtsCategory> queryCategoryPage1(Integer id, String name, Integer page, Integer limit, String sort, String order) {
        IPage<DtsCategory> ipage = new Page<>(page, limit);

        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted",false);
        queryWrapper.eq("pid",0);

        if (!StringUtils.isEmpty(id)){
            queryWrapper.eq("id",id);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.eq("name",name);
        }
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(sort);
            } else {
                queryWrapper.orderByDesc(sort);
            }
        }

        IPage<DtsCategory> resultPage = categoryMapper.selectPage(ipage, queryWrapper);

        return resultPage;
    }

    @Override
    public DtsCategory add(DtsCategory category) {
        category.setAddTime(new Date());
        category.setUpdateTime(new Date());
        categoryMapper.insert(category);
        return category;
    }

    @Override
    public void update(DtsCategory category) {
        category.setUpdateTime(new Date());
//        Integer id = category.getId();
        categoryMapper.updateById(category);

    }

    @Override
    public void delete(Integer id) {
        categoryMapper.deleteById(id);
    }



    @Override
    public List<CatVo> selectCategory() {
        List<CatVo> list = categoryMapper.selectDtsCategory();
        return list;
    }



    @Override
    public List<CatVo> selectcatVo11() {
        List<CatVo> list = categoryMapper.selectDtsCat();
        return list;
    }


}
